<?php

namespace app\api\controller;

use app\admin\model\Config;
use app\common\controller\ApiBase;
use app\Constant\ApiConstant;
use app\model\AdminAgent;
use app\model\AgentDistribution;
use app\model\DistributionSetting;
use app\model\Index;
use app\model\Swiper;
use app\model\Tutorial;
use app\model\TutorialCategory;
use app\service\UserService;
use think\facade\Request;

class Home extends ApiBase
{
    public function test_login()
    {
        $platform  = 'wxMiniProgram';
        $user_info = UserService::getUserInfo('ofbWc5Xd4t1Pcw5iM6xWUeFHOUcw', $platform);
        return $this->callback(ApiConstant::SUCCESS, 'success', $user_info);
    }

    /**
     * 获取首页分类
     * @return \think\response\Json
     */
    public function index()
    {
        $lists = Index::field('id, name, image, type, value, appid, app_path')
            ->withAttr('image', function ($value) {
                return $value ? get_file_path($value, 1) : '';
            })
            ->where('status', 1)
            ->order('sort', 'asc')
            ->select();
        return $this->callback(ApiConstant::SUCCESS, 'success', $lists);
    }

    /**
     * 获取轮播图
     * @return \think\response\Json
     */
    public function swiper()
    {
        $lists = Swiper::field('id, image, type, value, appid, app_path')
            ->withAttr('image', function ($value) {
                return $value ? get_file_path($value, 1) : '';
            })
            ->where('status', 1)
            ->order('sort', 'asc')
            ->select();
        return $this->callback(ApiConstant::SUCCESS, 'success', $lists);
    }

    /**
     * 获取公告列表
     * @return \think\response\Json
     */
    public function notice()
    {
        $type  = Request::param('type', 0);
        $query = \app\model\Notice::field('id, title, content, create_time, type');
        if ($type) {
            $query->where('type', $type);
        }
        $lists = $query
            ->where('status', 1)
            ->order('sort', 'asc')
            ->select();
        return $this->callback(ApiConstant::SUCCESS, 'success', $lists);
    }

    /**
     * 获取配置信息
     * @return \think\response\Json
     */
    public function config()
    {
        $setting         = [
            'kefu_qrcode', 'group_qrcode', 'official_qrcode', 'withdrawal_charge', 'withdrawal_price_min',
            'tutor_qrcode', 'wx_xcx_name', 'wx_xcx_logo', 'withdrawal_auto'
        ];
        $config          = Config::where('name', 'in', $setting)->column('name, value');
        $kefu_qrcode     = $config['kefu_qrcode'] ? get_file_path($config['kefu_qrcode'], 1) : '';
        $tutor_qrcode    = $config['tutor_qrcode'] ? get_file_path($config['tutor_qrcode'], 1) : '';
        $group_qrcode    = $config['group_qrcode'] ? get_file_path($config['group_qrcode'], 1) : '';
        $official_qrcode = $config['official_qrcode'] ? get_file_path($config['official_qrcode'], 1) : '';

        if ($this->token) {
            $user = \app\model\User::field('id, agent_id')->where('token', $this->token)->find();
            if (!empty($user['agent_id'])) {
                $agent = AdminAgent::field('a.id, m.name, m.kefu_qrcode, m.group_qrcode, m.official_qrcode')
                    ->alias('a')
                    ->leftJoin('agent_mini m', 'm.agent_id = a.id')
                    ->where('a.id', $user['agent_id'])
                    ->find();
                if ($agent['official_qrcode']) {
                    $official_qrcode = get_file_path($agent['official_qrcode'], 1);
                }
                if ($agent['group_qrcode']) {
                    $group_qrcode = get_file_path($agent['group_qrcode'], 1);
                }
                if ($agent['kefu_qrcode']) {
                    $kefu_qrcode = get_file_path($agent['kefu_qrcode'], 1);
                }
            }
        }
        $free_info = DistributionSetting::where('uuid', 'free')->value('value') ?: null;

        if ($free_info) {
            $free_info = json_decode($free_info, true);
            if (!empty($free_info['share'])) {
                $free_info['share'] = get_file_path($free_info['share'], 1);
            }
            if (!empty($free_info['pop'])) {
                $free_info['pop'] = get_file_path($free_info['pop'], 1);
            }
        }

        $res = [
            'kefu_qrcode'          => $kefu_qrcode,
            'tutor_qrcode'         => $tutor_qrcode,
            'group_qrcode'         => $group_qrcode,
            'official_qrcode'      => $official_qrcode,
            'withdrawal_charge'    => !empty($config['withdrawal_charge']) ? intval($config['withdrawal_charge']) : 0,
            'withdrawal_price_min' => !empty($config['withdrawal_price_min']) ? number_format($config['withdrawal_price_min'], 2) : "0.00",
            'free_setting'         => $free_info,
            'name'                 => $config['wx_xcx_name'],
            'logo'                 => $config['wx_xcx_logo'] ? get_file_path($config['wx_xcx_logo'], 1) : '',
            'withdrawal_auto'      => intval($config['withdrawal_auto']), //判断是否设置自动提现
        ];
        return $this->callback(ApiConstant::SUCCESS, 'success', $res);
    }

    /**
     * 入门教程
     * @return \think\response\Json
     */
    public function tutorial()
    {
        $category = TutorialCategory::field('id, name')
            ->where('status', 1)
            ->order('sort', 'asc')
            ->order('id', 'asc')
            ->select()->toArray();
        if ($category) {
            $lists = Tutorial::field('id, title, image, type, value, category_id, video_appid, video_path')
                ->withAttr('image', function ($value) {
                    return $value ? get_file_path($value, 1) : '';
                })
                ->where('status', 1)
                ->order('sort', 'asc')
                ->order('id', 'asc')->select()->toArray();

            $index_tutorial = [];
            foreach ($lists as $item) {
                if ($item['type'] == 1 || $item['type'] == 2) {
                    $value = get_file_path($item['value'], 1);
                } else {
                    $value = $item['video_path'];
                }
                $index_tutorial[$item['category_id']][] = [
                    'id'    => $item['id'],
                    'title' => $item['title'],
                    'image' => $item['image'],
                    'type'  => $item['type'],
                    'value' => $value,
                    'appid' => $item['video_appid'],
                ];
            }
            foreach ($category as &$item) {
                $item['tutorial'] = $index_tutorial[$item['id']] ?? [];
            }
        }

        return $this->callback(ApiConstant::SUCCESS, 'success', $category);
    }

    /**
     * 推客升级
     * @return \think\response\Json
     */
    public function tk_upgrade()
    {
        $info = DistributionSetting::field('task_condition, invite, income')
            ->where('uuid', 'setting')
            ->withAttr('income', function ($value) {
                return $value > 0 ? $value / 100 : 0;
            })
            ->find();
        if ($this->token) {
            $user = \app\model\User::field('id, agent_id')->where('token', $this->token)->find();
            if ($user['agent_id']) {
                $agent_info             = AgentDistribution::field('task_condition, invite, income')->where('agent_id', $user['agent_id'])->find();
				$agent_info['level'] = json_decode($agent_info['task_condition'], true);
                $info['task_condition'] = $agent_info['task_condition'];
                $info['invite']         = $agent_info['level']['invite'];
                $info['income']         = $agent_info['income'] > 0 ? $agent_info['income'] / 100 : 0;
            }
        }
        return $this->callback(ApiConstant::SUCCESS, 'success', $info);
    }
}
